﻿Imports Publics
Imports System.Windows.Forms

Public Class FrmCapNhatPhimTat
    Private PhimChon As Integer
    Private _PhimTat As SYS_PhimTat
    Public Property PhimTat() As SYS_PhimTat
        Get
            Return _PhimTat

        End Get
        Set(ByVal value As SYS_PhimTat)
            _PhimTat = value
        End Set
    End Property

    Private _ActionForm As String
    Public Property ActionForm() As String
        Get
            Return _ActionForm
        End Get
        Set(ByVal value As String)
            _ActionForm = value
        End Set
    End Property

    ''' <summary>
    ''' Kiểm tra ký tự phím tắt đã tồn tại hay chưa
    ''' @NhiLV
    ''' </summary>
    ''' <param name="phim">Phím tắt</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function CheckSave(ByVal phim As SYS_PhimTat) As String
        Dim strError As String = String.Empty
        If String.IsNullOrEmpty(phim.MaNut) Then
            strError = "Bạn chưa nhập mã nút"
        ElseIf phim.KeyCode = 0 Or phim.KeyCode Is Nothing Then
            strError = "Bạn chưa nhập phím tắt"
        ElseIf txtNhom.Text.Trim = "" Then
            strError = "Bạn chưa nhập nhóm"
        End If 
        If ActionForm = vAddAction Then 
            Dim objData = (From ds In dbTV.SYS_PhimTats Where ds.Nhom = txtNhom.Text.Trim AndAlso ds.MaNut = phim.MaNut Select ds).FirstOrDefault
            Dim objData1 = (From ds In dbTV.SYS_PhimTats Where ds.Nhom = txtNhom.Text.Trim AndAlso ds.KeyFn = phim.KeyFn And ds.KeyCode = phim.KeyCode Select ds).FirstOrDefault
            If objData IsNot Nothing Then
                strError = "Trùng mã nút"
            ElseIf objData1 IsNot Nothing Then
                strError = "Trùng phím tắt"
            End If
        ElseIf ActionForm = vEditAction Then
            Dim objData As SYS_PhimTat = (From ds In dbTV.SYS_PhimTats Where ds.KeyFn = phim.KeyFn _
                                          AndAlso ds.KeyCode = phim.KeyCode AndAlso ds.Nhom = txtNhom.Text.Trim Select ds).FirstOrDefault()

            If objData IsNot Nothing Then
                If objData.MaNut <> phim.MaNut Then
                    strError = "Trùng mã nút"
                End If
            End If
        End If

        Return strError
    End Function

    Private Sub btnHuyBo_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnHuyBo.ItemClick
        Me.Close()
    End Sub

    Private Sub btnGhiVaDong_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnGhiVaDong.ItemClick
        Try
            If ActionForm = vEditAction Then
                If PhimTat IsNot Nothing Then
                    Select Case rgPhimFn.EditValue
                        Case 0
                            PhimTat.KeyFn = 0
                        Case 1
                            PhimTat.KeyFn = Keys.Control
                        Case 2
                            PhimTat.KeyFn = Keys.Alt
                        Case 3
                            PhimTat.KeyFn = Keys.Shift

                    End Select
                    PhimTat.KeyCode = PhimChon
                    PhimTat.DienGiai = txtDienGiai.Text
                    PhimTat.TenNut = txtPhim.Text
                End If
                Dim strError As String = CheckSave(PhimTat)
                If Not String.IsNullOrEmpty(strError) Then
                    MsgInfo(strError)
                    Exit Sub
                End If
                iSubmitChange()
                ActionForm = vUpdateAction
                Me.Close()
            Else

                Dim a As New SYS_PhimTat
                a.InUsed = True
                a.KeyCode = PhimChon
                Select Case rgPhimFn.EditValue
                    Case 0
                        a.KeyFn = 0
                    Case 1
                        a.KeyFn = Keys.Control
                    Case 2
                        a.KeyFn = Keys.Alt
                    Case 3
                        a.KeyFn = Keys.Shift

                End Select
                a.DienGiai = txtDienGiai.Text
                a.MaNut = txtMa.Text
                a.TenNut = txtPhim.Text
                dbTV.SYS_PhimTats.InsertOnSubmit(a)
                Dim strError As String = CheckSave(a)

                If strError <> "" Then
                    MsgInfo(strError)
                    Exit Sub
                End If

                iSubmitChange()
                Me.Close()
            End If
        Catch ex As Exception
            MsgInfo(ex.Message)
        End Try
    End Sub

    Private Sub FrmCapNhatPhimTat_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
        If e.KeyChar = Chr(Keys.Enter) Then
            SendKeys.Send("{tab}")
        End If
    End Sub

    ''' <summary>
    ''' Phím tắt
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub FrmCapNhatPhimTat_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        'Huỷ bỏ (thoát)
        If (e.Modifiers = GetKeyModifide(pIdThoat) And _
     e.KeyCode = GetKeyCode2(pIdThoat)) Or _
     (e.KeyCode = GetKeyCode(pIdThoat)) Then
            btnHuyBo_ItemClick(Nothing, Nothing)
        End If
        'Ghi và đóng
        If (e.Modifiers = GetKeyModifide(pIdGhiVaDong) And _
            e.KeyCode = GetKeyCode2(pIdGhiVaDong)) Or _
            (e.KeyCode = GetKeyCode(pIdGhiVaDong)) Then
            If Me.btnGhiVaDong.Enabled = True And Me.btnGhiVaDong.Visibility = DevExpress.XtraBars.BarItemVisibility.Always Then
                btnGhiVaDong_ItemClick(Nothing, Nothing)
            End If

        End If
        'Trợ giúp
        If (e.Modifiers = GetKeyModifide(pIdTroGiup) And _
    e.KeyCode = GetKeyCode2(pIdTroGiup)) Or _
    (e.KeyCode = GetKeyCode(pIdTroGiup)) Then

            If Me.btnTroGiup.Enabled = True And Me.btnTroGiup.Visibility = DevExpress.XtraBars.BarItemVisibility.Always Then
                bntTroGiup_ItemClick(Nothing, Nothing)
            End If

        End If

    End Sub

    Private Sub txtPhimChon_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub FrmCapNhatPhimTat_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        btnGhiVaDong.Caption = BtnSaveAndCloseCaption
        btnHuyBo.Caption = BtnCancelCaption
        btnTroGiup.Caption = BtnHelpCaption
        Me.Text = "Cập nhật phím tắt"
        If ActionForm = vAddAction Then
            txtMa.Properties.ReadOnly = False
        ElseIf ActionForm = vEditAction Then
            If PhimTat IsNot Nothing Then
                txtMa.Text = PhimTat.MaNut

                Select Case PhimTat.KeyFn
                    Case Keys.Shift
                        rgPhimFn.SelectedIndex = 3
                    Case Keys.Control
                        rgPhimFn.SelectedIndex = 1
                    Case Keys.Alt
                        rgPhimFn.SelectedIndex = 2
                    Case Else
                        rgPhimFn.SelectedIndex = 0

                End Select
                txtNhom.Text = PhimTat.Nhom
                txtNhom.Properties.ReadOnly = True
                txtNhom.TabStop = True
                txtPhimChon.Text = KeyCodeToChar(PhimTat.KeyCode)
                PhimChon = PhimTat.KeyCode
                txtDienGiai.Text = PhimTat.DienGiai
                txtPhim.Text = PhimTat.TenNut
            End If
        End If

    End Sub

    Private Sub rgPhimFn_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles rgPhimFn.KeyDown
        Select Case e.KeyCode
            Case Keys.Control
                rgPhimFn.EditValue = 1
            Case Keys.Alt
                rgPhimFn.EditValue = 2
            Case Keys.Shift
                rgPhimFn.EditValue = 3
        End Select
    End Sub

    Private Sub btnChonPhim_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChonPhim.Click

        Dim frm As New FrmGetKey
        If PhimTat IsNot Nothing Then frm.KeyValue = PhimTat.KeyCode
        frm.ShowDialog()
        If Not String.IsNullOrEmpty(frm.KeyData) Then
            txtPhimChon.Text = frm.KeyData
            PhimChon = frm.KeyValue
            Select Case rgPhimFn.EditValue
                Case 0
                    txtPhim.Text = txtPhimChon.Text
                Case 1
                    txtPhim.Text = "Ctrl & " & txtPhimChon.Text
                Case 2
                    txtPhim.Text = "Alt & " & txtPhimChon.Text
                Case 3
                    txtPhim.Text = "Shift & " & txtPhimChon.Text
            End Select
            txtDienGiai.Focus()
        End If


    End Sub

    Private Sub rgPhimFn_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rgPhimFn.EditValueChanged
        Select Case rgPhimFn.EditValue
            Case 0
                txtPhim.Text = txtPhimChon.Text
            Case 1
                txtPhim.Text = "Ctrl & " & txtPhimChon.Text
            Case 2
                txtPhim.Text = "Alt & " & txtPhimChon.Text
            Case 3
                txtPhim.Text = "Shift & " & txtPhimChon.Text
        End Select
    End Sub

    Private Sub bntTroGiup_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs)

    End Sub
End Class